/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.fd.controller;

import java.util.Collection;
import java.util.List;

import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.fd.domain.resp.GetDocumentLogResponse;
import com.xy.biz.fd.domain.req.GetDocumentLogRequest;
import com.xy.biz.fd.domain.req.ListDocumentLogRequest;
import com.xy.biz.fd.domain.req.SaveDocumentLogRequest;
import com.xy.biz.fd.domain.query.DocumentLogQuery;
import com.xy.biz.fd.domain.converter.DocumentLogConverter;
import com.xy.biz.fd.client.DocumentLogClient;
import com.xy.biz.fd.service.DocumentLogService;

/**
 * 
 * 
 * @author zgy
 * @date 2021-5-20
 */
@Api(tags = "文件操作记录服务")
@Slf4j
@RestController
@RequestMapping("/api/fd/document_log")
public class DocumentLogController implements DocumentLogClient {

	@Autowired
	private DocumentLogService documentLogService;
	
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveDocumentLog(@RequestBody SaveDocumentLogRequest request) {
		documentLogService.saveDocumentLog(DocumentLogConverter.toBO(request));
		return Result.success();
	}
	
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllDocumentLog(@RequestBody Collection<SaveDocumentLogRequest> collection) {
		documentLogService.saveAllDocumentLog(DocumentLogConverter.toBO(collection));
		return Result.success();
	}
	
	/*@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateDocumentLog(@RequestBody SaveDocumentLogRequest request) {
		documentLogService.updateDocumentLog(DocumentLogConverter.toBO(request));
		return Result.success();
	}
	
	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteDocumentLog(@RequestBody GetDocumentLogRequest request) {
		documentLogService.deleteDocumentLog(request.getDocumentLogId());
		return Result.success();
	}*/

	/**
	 * 查找一个
	 *
	 * @param request
	 * @return
	 */
	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetDocumentLogResponse> getDocumentLog(GetDocumentLogRequest request) {
		return Result.success(DocumentLogConverter.toResponse(documentLogService.getDocumentLog(request.getDocumentLogId())));
	}

	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetDocumentLogResponse>> listDocumentLogById(@RequestBody Collection<Long> collection) {
		return Result.success(DocumentLogConverter.toResponse(documentLogService.listDocumentLogById(collection)));
	}

	/**
	 * 查找多个
	 *
	 * @param request 查找条件
	 * @return
	 */
	@ApiOperation("根据文档Id获取文档操作记录")
	@PostMapping(value = "/list")
	@Override
	public Result<List<GetDocumentLogResponse>> listDocumentLog(@RequestBody ListDocumentLogRequest request) {
		return Result.success(DocumentLogConverter.toResponse(documentLogService.listDocumentLog(DocumentLogConverter.toQuery(request))));
	}

	/**
	 * 统计个数
	 *
	 * @param request 查找条件
	 * @return
	 */
	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countDocumentLog(ListDocumentLogRequest request) {
		return Result.success(documentLogService.countDocumentLog(DocumentLogConverter.toQuery(request)));
	}

	/**
	 * 分页查找
	 *
	 * @param request 查找条件
	 * @return
	 */
	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetDocumentLogResponse>> pageDocumentLog(ListDocumentLogRequest request) {
		DocumentLogQuery query = DocumentLogConverter.toQuery(request);
		List<GetDocumentLogResponse> list = DocumentLogConverter.toResponse(documentLogService.listDocumentLog(query));
		int count = documentLogService.countDocumentLog(query);
		return Result.success(new Page<>(list, count));
	}

}
